<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>实例4-2</title>
  </head>
  <body>
    <script type="text/javascript">
      var data = {
        a: "1111",
        b: "2222",
      };
      var data1 = [
        { name: "zsh", age: "12" },
        { name: "zzz", age: "23" },
        { name: "wer", age: "67" },
      ];
      // 存储值：将对象转换为Json字符串
      sessionStorage.setItem("user", JSON.stringify(data));
      sessionStorage.setItem("user1", JSON.stringify(data1));
      // 取值时：把获取到的Json字符串转换回对象
      var getdata = JSON.parse(sessionStorage.getItem("user"));
      var getdata1 = JSON.parse(sessionStorage.getItem("user1"));
      console.log(getdata); //{a: "1111", b: "2222"}
      console.log(getdata1); //返回数组
      sessionStorage.clear();
      var getdata2 = JSON.parse(sessionStorage.getItem("user1"));
      console.log(getdata2); //null
    </script>
  </body>
</html>
